import BasicTool from '../BasicTool'; 

/**
 * 绘制点
 */
export default class DrawPoint {
    /**
     * 构造函数
     * @param viewer
     */
    constructor(viewer) {
        this.viewer = viewer;
        this._drawHandler = null;//事件
        this._dataSource = null;//存储entities
        this._tempPositions = [];//存储点集合
        this._mousePos = null;//移动点
        this._drawType = "Point";
        this.basicTool = new BasicTool(viewer)
        this.drawRectangleTool = null;
    }
 
    /**
     * 激活
     */
    activate(callback) {
        this.clearAll();
        this.callback = callback;
        /**
         * 回调结果
         * type：类型       Point-点 Polyline-线 Polygon-面
         * state： 状态     drawing-绘制中  finish-绘制完成
         * points：经纬度  这个是用于保存绘制点时单独返回点           格式是[113.03,23.03,100]
         * points：经纬度集合           格式是[[113.03,23.03,100]]
         * positions：cesium 的点对象集合    格式是cesium的Cartesian对象 绘制类型是点的话，这里就是单独对象，不是数组
         */
        this.callbackResult= {
            type: this._drawType,
            state: "",
            point: null,
            points: [],
            positions: []
        };
        this._dataSource = new Cesium.CustomDataSource('point_dataSource');
        this.viewer.dataSources.add(this._dataSource);
        this._registerEvents(); //注册鼠标事件
    }
 
    /**
     * 注册鼠标事件
     */
    _registerEvents() {
        this._drawHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
        this.viewer.scene.globe.depthTestAgainstTerrain = true;//开启深度测试
        this._leftClickEventForPoint();
    }
 
    /**
     * 鼠标事件之绘制点的左击事件
     * @private
     */
    _leftClickEventForPoint() {
        this._drawHandler.setInputAction((e) => {
            // this.viewer._element.style.cursor = 'default';
            let p = this.viewer.scene.pickPosition(e.position)
            if (!p) return;
            let carto_pt = Cesium.Cartographic.fromCartesian(p);
            let p1 = [Cesium.Math.toDegrees(carto_pt.longitude), Cesium.Math.toDegrees(carto_pt.latitude), carto_pt.height+1]
            // 回调
            this.callbackResult = {
                type: this._drawType,
                state: "finish",
                point: p1,
                positions: p
            }
            this.callback && this.callback(this.callbackResult)
            var point = this._addPoint(p1);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
    }
 
    /**
     * 移除所有鼠标事件
     * @private
     */
    _removeAllEvent() {
        this._drawHandler && (
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK),
            this._drawHandler.destroy(),
            this._drawHandler = null
        )
    }
 
    /**
     * 重置所有参数
     * @private
     */
    _resetParams() {
        if (this._dataSource != null) {
            this._dataSource.entities.removeAll();
            this.viewer.dataSources.remove(this._dataSource);
        }
        this._dataSource = null;
        this._tempPositions = [];
        this._mousePos = null;
        this._drawType = null;
    }
 
    /**
     * 清除
     */
    clearAll() {
        this._removeAllEvent();
        this._resetParams();
    }
 
    /**
     * 画点
     * @param p
     * @private
     */
    _addPoint(p) {
        return this._dataSource.entities.add({
            position: Cesium.Cartesian3.fromDegrees(p[0], p[1], p[2]),
            point: {
                color: Cesium.Color.RED,
                pixelSize: 10,
                outlineColor: Cesium.Color.YELLOW,
                outlineWidth: 2,
                // heightReference:Cesium.HeightReference.CLAMP_TO_GROUND
            }
        })
    }
}
